package juc;

public class Demo2 {
    public static void main(String[] args) {
        //多个线程操作统一资源Car
        Car car = new Car();

        //1的司机开这辆车
        new Thread(()->{
            for(int i = 0; i < 20 ; i++){
                car.driverThisBus(1);
            }
        },"A").start();

        //2的司机开这辆车
        new Thread(()->{
            for(int i = 0; i < 20 ; i++){
                car.driverThisBus(2);
            }
        },"B").start();

        //3的司机开这辆车
        new Thread(()->{
            for(int i = 0; i < 20 ; i++){
                car.driverThisBus(3);
            }
        },"C").start();

    }
}

class Car{
    private String carCode = "闽D66666";

    private int id = 0;

    //该变量为了查看锁的影响
    private int time = 0;



    //不加Synchronized:无序性
    public synchronized void driverThisBus(int a){
        id = a;
        System.out.println(id + "开这辆车：" + carCode+ "。"+time++);
    }
}
